package xrc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
//import org.thymeleaf.util.StringUtils;
import org.springframework.util.StringUtils;
import xrc.exception.GlobalException;
import xrc.mapper.UserMapper;
import xrc.pojo.User;
import xrc.service.IUserService;
import xrc.utils.CookieUtil;
import xrc.utils.MD5Util;
import xrc.utils.UUIDUtil;
import xrc.vo.LoginVo;
import xrc.vo.RespBean;
import xrc.vo.RespBeanEnum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xrc
 * @since 2023-07-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    // 登录
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        // 由于已经自定义手机号码校验 所以下段代码可以省略
        /*if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        if (!ValidatorUtil.isMobile(mobile)) {
            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        }*/

        // 根据手机号获取用户
        User user = userMapper.selectById(mobile);
        if (user == null) {
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        // 判断密码是否正确
        if (!MD5Util.formPassToDBPass(password, user.getSalt()).equals(user.getPassword())) {
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        // 生成cookie
        String ticket = UUIDUtil.uuid();
        // 此时需要将 cookie 存进 session 中 详见 LoginController 类
//        request.getSession().setAttribute(ticket, user);

        // 将用户信息存入redis中
        redisTemplate.opsForValue().set("user:" + ticket, user);
        CookieUtil.setCookie(request, response, "userTicket", ticket);
        System.out.println("ticket = " + ticket);
        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket,
                                HttpServletRequest request, HttpServletResponse response) {

        if (StringUtils.isEmpty(userTicket)) {
            return null;
        }
        // 根据userTicket获取用户对象信息
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        if (user != null) {
            // 在客户端浏览器上存储一个名为"userTicket"的 Cookie 并将其中的值设置为"userTicket"变量的值
            CookieUtil.setCookie(request, response, "userTicket", userTicket);
        }
        return user;
    }

    // 更新密码
    @Override
    public RespBean updatePassword(String userTicket, String password,
                                   HttpServletRequest request, HttpServletResponse response) {
        User user = getUserByCookie(userTicket, request, response);
        if (user == null) {
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        }
        user.setPassword(MD5Util.inputPassToDBPass(password, user.getSalt()));
        int result = userMapper.updateById(user);
        if (result == 1) {
            // 删除redis
            redisTemplate.delete("user:" + userTicket);
            return RespBean.success();
        } else {
            return RespBean.error(RespBeanEnum.DELETE_ERROR);
        }

    }
}
